<template>
	<view class="content" style="background: url(../../static/diary/bg.png);background-size: 100%  100%;">
		<!-- <view class="flex" style="z-index:1;width: 100%;position: fixed;left: 0;top:160px;">
			<view @click="select(item, index)" v-for="(item, index) in datalist" class="w50"
				style="text-align: center;position: relative;">
				<view style="position: absolute;z-index: 5;top:-10px;left: 0px;">
					<view style="position: absolute;z-index: 1;top:20px;left: -2px;width: 100%;height: 100%;">
						<view style="font-size: 12px;color: #fec5ce;transform:rotate(-30deg);" class="fb">{{ item.name}}
						</view>
					</view>
					<image src="https://yunkeososyunchuc.bc8t.cn/lianaikongjian/static/yun.png" mode="widthFix"
						style="width: 70px;"></image>
				</view>

				<image style="width:100%;z-index: 2;"
					:src="num == index ? 'https://yunkeososyunchuc.bc8t.cn/lianaikongjian/static/kuang1.png' : 'https://yunkeososyunchuc.bc8t.cn/lianaikongjian/static/kuang.png'"
					mode="widthFix"></image>
				<image :src="item.cover_image" mode="widthFix"
					style="width: 50%;position: absolute;z-index: 1;left: 25%;top: 25%;"></image>
			</view>
		</view>

		<image @click="gourl('/pages/kongjian/kongjian?item=' + JSON.stringify(datalist[num]))"
			src="https://yunkeososyunchuc.bc8t.cn/lianaikongjian/static/button.png"
			style="width: 50%; position: fixed; z-index: 1; bottom: 80px; left: 50%; margin-left: -25%;  "
			mode="widthFix"></image> -->
		<view class="btn" @click="gourl('/pages/kongjian/kongjian?item=' + JSON.stringify(datalist[num]))" style="background: url(../../static/index/btn-one.png);background-size: 100%  100%;">
			邀请您的另一半
		</view>
		<view class="btn" style="background: url(../../static/index/btn-two.png);background-size: 100%  100%;">
			我是单身，寻解救
		</view>

		<u-popup v-model="showUserModal" border-radius="20" mode="bottom">
			<view class="slot-content">
				<view class="user-info-box">
					<button class="avatar-wrapper" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
						<image class="avatar" :src="userInfoForm.avatarUrl"></image>
					</button>
					<u-form :model="userInfoForm" ref="uForm">
						<u-form-item label="姓名">
							<u-input type="nickname" id="nickname-input" @input="onChooseNickanme"
								@blur="onChooseNickanme" v-model="userInfoForm.nickName" placeholder="请输入姓名" />
						</u-form-item>
						<u-form-item label="性别">
							<u-select v-model="genderModal" :list="genderList" @confirm="genderConfirm"></u-select>
							<u-input v-model="userInfoForm.genderText" type="select" @click="genderModal=true"
								placeholder="请选择性别" />
						</u-form-item>
					</u-form>
					<view class="button-style">
						<u-button type="default" @click="submitUserInfo">确认</u-button>
					</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				title: 'Hello',
				datalist: [],
				num: 0,
				userinfo: this.vuex_user,
				type: '',
				code: '',
				state: '',
				auth_url: undefined,
				showUserModal: false,
				genderModal: false,
				genderList: [{
						value: '1',
						label: '男'
					},
					{
						value: '2',
						label: '女'
					}
				],
				userInfoForm: {
					avatarUrl: null,
					nickName: null,
					gender: null,
					genderText: null,
				},
			};
		},
		async onLoad(e) {
			// #ifdef H5
			const url = await this.$u.api.getAuthUrl();
			this.auth_url = url;
			const weixin_code = this.getParam(location.href, 'code');
			if (weixin_code) {
				this.code = weixin_code;
				let wixinstate = this.getParam(location.href, 'state');
				this.state = wixinstate;
			}
			// #endif

			const {
				token
			} = e;
			if (token) {
				this.$u.vuex('vuex_token', token);

				const user_info = await this.$u.api.uinfo();
				this.$u.vuex('vuex_user', user_info);
			}
			this.list();
			if (e.type) {
				this.type = e.type;
			}

		},
		onShow() {},
		methods: {
			getParam(path, name) {
				var reg = new RegExp('(^|\\?|&)' + name + '=([^&]*)(\\s|&|$)', 'i');
				if (reg.test(path)) return unescape(RegExp.$2.replace(/\+/g, ' '));
				return '';
			},
			async list() {
				let res = await this.$u.api.setspaceurl();
				this.datalist = res;
				uni.setStorageSync('list', JSON.stringify(res));
			},
			gourl(e) {
				if (!this?.vuex_user?.nickname) {
					// #ifdef MP-TOUTIAO
					this.user().then(r => {
						this.$u.api.getUserInfo(r).then(re => {
							this.$u.vuex('vuex_user', re);
							uni.navigateTo({
								url: e
							});
						});
					});
					// #endif

					// #ifdef MP-WEIXIN
					if (!this.vuex_user.avatar) {
						this.showUserModal = true;
						return false;
					}
					// #endif

					// #ifdef H5
					window.location.href = this.auth_url;
					// #endif
				} else {
					uni.navigateTo({
						url: e
					});
				}
			},
			async select(item, e) {
				this.num = e;
			},
			onChooseNickanme(res) {
				if (res) {
					this.userInfoForm.nickName = res
				}
			},
			async onChooseAvatar(res) {
				const path = await this.uploader(res.detail.avatarUrl);
				this.userInfoForm.avatarUrl = path
			},
			genderConfirm(res) {
				this.userInfoForm.gender = res[0].value
				this.userInfoForm.genderText = res[0].label
				this.genderModal = false;
			},
			async submitUserInfo() {
				if (!this.userInfoForm.avatarUrl) {
					uni.showToast({
						title: '请选择头像',
						icon: 'none',
					});
					return false;
				}

				if (!this.userInfoForm.nickName) {
					uni.showToast({
						title: '请输入姓名',
						icon: 'none',
					});
					return false;
				}

				if (!this.userInfoForm.gender) {
					uni.showToast({
						title: '请选择性别',
						icon: 'none',
					});
					return false;
				}

				const user = await this.$u.api.getUserInfo(this.userInfoForm);
				this.$u.vuex('vuex_user', user);
				this.showUserModal = false;
			},
		}
	};
</script>

<style scoped>
	.content {
		width: 100%;
		height: 100vh;
	}

	.user-info-box {
		width: 100%;
		height: 600rpx;
		background-color: white;
		padding: 80rpx;
	}

	.avatar-wrapper {
		padding: 0;
		width: 56px !important;
		border-radius: 8px;
	}

	.avatar {
		display: block;
		width: 56px;
		height: 56px;
	}
	.btn {
		position: absolute;
		left: 50%;
		bottom: 250rpx;
		transform: translateX(-50%);
		width: 90%;
		height: 150rpx;
		text-align: center;
		display: flex;
		align-items: center;
		justify-content: center;
		color: #fff;
	}
	.btn:nth-child(2) {
		bottom: 120rpx;
	}
</style>